<?php

/**
 * 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用,
 * 这些方法, 都接受一个参数:Yaf_Dispatcher $dispatcher
 * 调用的次序, 和申明的次序相同
 */
class Bootstrap extends Yaf_Bootstrap_Abstract{

    //开始session
    public function _initSession(){
        
        Yaf_Session::getInstance()->start();
        defined('AK47') ? '':define('AK47', 'YM_Admin');
    }

    public function _initConfig() {
        Yaf_Registry::set('config', Yaf_Application::app()->getConfig());  
        // Yaf_Dispatcher::getInstance()->autoRender(FALSE);
    } 
    /**
     * [_initCommonFunctions 加载公共函数文件]
     * @return [type] [description]
     */
    public function _initCommonFunctions(){
        Yaf_Loader::import(Yaf_Application::app()->getConfig()->application->directory . '/common/functions.php');
    }

    /**
     * 添加自定义路由信息
     */
    public function _initRoute(Yaf_Dispatcher $dispatcher){
        $router = $dispatcher->getRouter();
        $route = new Yaf_Route_Regex(
            '#/news(-([a-zA-Z-_0-9]+)-([a-zA-Z-_0-9]+).html)?$#',
            array(
                'module'        =>  'Index',
                'controller'    =>  'Index',
                'action'        =>  'news'
            ),
            array(
                2=>'_id',
                3=>'id'
                )
        );
        $router->addRoute('news_default', $route);
        $route = new Yaf_Route_Regex(
            '#/detail-([a-zA-Z-_0-9]+).html$#',
            array(
                'module'        =>  'Index',
                'controller'    =>  'Index',
                'action'        =>  'aboutus'
            ),
            array(
                1=>'about_id',
                )
        );
        $router->addRoute('about_us_default', $route);

        // background default url
        $route = new Yaf_Route_Regex(
            '#/admin.html$#',
            array(
                'module'        =>  'Admin',
                'controller'    =>  'login',
                'action'        =>  'login'
            ),
	    array()
        );
        $router->addRoute('admin_default', $route);
    }
    /**
     * [_initSmarty 整合Smarty模板引擎]
     * @param  Yaf_Dispatcher $dispatcher [description]
     * @return [type]                     [description]
     */
    public function _initSmarty(Yaf_Dispatcher $dispatcher){
        $smarty = new Smarty_Adapter(null , Yaf_Application::app()->getConfig()->smarty);
        Yaf_Dispatcher::getInstance()->setView($smarty);
    }

    /**
     * 插件使用
     * @param  Yaf_Dispatcher $dispatcher [description]
     * @return [type]                     [description]
     */
	public function _initPlugin(Yaf_Dispatcher $dispatcher) {
        // $user = new UserPlugin();
        // $dispatcher->registerPlugin($user);
    }

}
    
